package shop.core.plugins.wxmsg;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.entity.StringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import shop.core.plugins.wxlogin.AccessToken;
import shop.core.plugins.wxlogin.WxUrlType;
import shop.core.plugins.wxlogin.WxUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 微信发送模板消息
 * @Author semstouch
 * @Date 2017/7/16
 **/
public class WxMsgUtil {
    protected static final Logger logger = LoggerFactory.getLogger(WxMsgUtil.class);

    /**
     * 模板消息通知：付款成功
     *
     * @param param       要发送的信息内容
     * @param accessToken 微信公众号accessToken
     * @param openid      需要接收的微信号openid
     * @return
     */
    public static boolean sendPaySuccessMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "6QnW9GIY7GLhnZSHO8vYPlKmse8CTMpsoEeRX0E6geQ";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "http://agent.finebeiy.com/rest/front/order/toOrderList");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：付款失败（未付款）
     *
     * @param param
     * @param accessToken
     * @param openid
     * @return
     */
    public static boolean sendPayFailedMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "nZR8Dh3YFAXJsmGCfWaF1qOKcPW99jlWCsu3nutDz5Q";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "http://agent.finebeiy.com/rest/front/order/toOrderList");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：发货
     *
     * @param param
     * @param accessToken
     * @param openid
     * @return
     */
    public static boolean sendProductMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "IXQjDia7WMZq0A2Y0nCGOWW3ifmPXFBMwYHjyxSjcJc";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "http://agent.finebeiy.com/rest/front/order/toOrderList");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }


    /**
     * 模板消息通知：下级用户下单
     *
     * @param param
     * @param accessToken
     * @param openid
     * @return
     */
    public static boolean sendSubOrderMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "dtXoBjVi8v2GvO8R9eHYwEJO0aow2EzM1uWiiFd7la8";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "http://agent.finebeiy.com/rest/front/entrepot/toMyDepot");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：确认收货(上级或平台)
     *
     * @param param
     * @param accessToken
     * @param openid
     * @return
     */
    public static boolean sendOrderFinishMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "yyKlwBm7buIPw3sHckmZ9W9eZTONacMlQ70qg29oGH4";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "http://agent.finebeiy.com/rest/front/entrepot/toMyDepot");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }


    /**
     * 模板消息通知：确认收货(下级用户)
     *
     * @param param
     * @param accessToken
     * @param openid
     * @return
     */
    public static boolean sendSubOrderFinishMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "o7JahwoaUqGEkDmsN80BHUx-c8iUjapnGJ2jiZ4ZSUw";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "http://agent.finebeiy.com/rest/front/order/toOrderList");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }


    /**
     * 模板消息通知：用户成功升级会员
     *
     * @param param
     * @param accessToken
     * @param openid
     * @return
     */
    public static boolean sendRankFinishMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "jShec73Tz6lPBex7Sf7IM-iSBGCZNOzGY1Xlc-40vAc";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "http://agent.finebeiy.com/rest/front/order/toOrderList");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：提现审核结果
     *
     * @param param
     * @param accessToken
     * @param openid
     * @return
     */
    public static boolean sendWalletCheckMsg(Map<String, String> param, AccessToken accessToken, String openid) {
        String templateId = "NBpTnlhSo81s8HIOAhtgibHpblGVihKoJ2ChyWRsshM";   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", "");

        Map msgParam = new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", accessToken.getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }


}
